/*
* render simple obj
*/
#pragma once

#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "vortex/mesh/mesh.h"
#include "vortex/mesh/sphere.h"
#include "vortex/core/utils.h"
#include <memory>
#include <GLFW/glfw3.h>
#include "vortex/core/camera.h"
#include "vortex/renderer/gl/shader.h"
#include "vortex/mesh/cube.h"

namespace vortex
{
    class SimpleScene
    {
    private:
        glm::vec3 light_pos;
        std::shared_ptr<Camera> camera;
        std::shared_ptr<Shader> object_shader;
        std::shared_ptr<Shader> light_shader;
        std::shared_ptr<Cube> cube;
        std::shared_ptr<Cube> light;

    public:
        SimpleScene()
        {
            init_resources();
        }

        void init_resources()
        {
            cube = std::make_shared<Cube>();
            light = std::make_shared<Cube>();
            object_shader = std::make_shared<Shader>(
                getResourcePath("shaders/test/1.colors.vs").c_str(),
                getResourcePath("shaders/test/1.colors.fs").c_str());

            light_shader = std::make_shared<Shader>(
                getResourcePath("shaders/test/1.light_cube.vs").c_str(),
                getResourcePath("shaders/test/1.light_cube.fs").c_str());
            camera = std::make_shared<Camera>(glm::vec3(0.0f, 0.0f, 3.0f));
            light_pos = glm::vec3(1.2f, 1.0f, 2.0f);
        }

        void update(float dt)
        {
            
        }

        void display()
        {
            // render
            // ------
            glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

            // be sure to activate shader when setting uniforms/drawing objects
            object_shader->use();
            object_shader->setVec3("objectColor", 1.0f, 0.5f, 0.31f);
            object_shader->setVec3("lightColor", 1.0f, 1.0f, 1.0f);

            // view/projection transformations
            glm::mat4 projection = glm::perspective(
                glm::radians(camera->Zoom), 800.0f / 600.0f,
                0.1f, 100.0f);
            glm::mat4 view = camera->GetViewMatrix();
            object_shader->setMat4("projection", projection);
            object_shader->setMat4("view", view);

            // world transformation
            glm::mat4 model = glm::mat4(1.0f);
            object_shader->setMat4("model", model);

            // render the cube

            // glBindVertexArray(cubeVAO);
            cube->bind();
            glDrawArrays(GL_TRIANGLES, 0, 36);

            // also draw the lamp object
            light_shader->use();
            light_shader->setMat4("projection", projection);
            light_shader->setMat4("view", view);
            model = glm::mat4(1.0f);
            model = glm::translate(model, light_pos);
            model = glm::scale(model, glm::vec3(0.2f)); // a smaller cube
            light_shader->setMat4("model", model);

            // glBindVertexArray(lightCubeVAO);
            light->bind();
            glDrawArrays(GL_TRIANGLES, 0, 36);

        }
    };
}
